package com.yfp.operation.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import com.yfp.common.core.domain.R;
import com.yfp.common.core.utils.poi.ExcelUtil;
import com.yfp.common.core.web.controller.BaseController;
import com.yfp.common.core.web.page.TableDataInfo;
import com.yfp.common.log.annotation.Log;
import com.yfp.common.log.enums.BusinessType;
import com.yfp.operation.domain.dto.*;
import com.yfp.operation.domain.vo.*;
import com.yfp.operation.service.ComboService;
import com.yfp.operation.service.MarAssociationService;
import com.yfp.operation.service.OrderListService;
import com.yfp.operation.service.StatisticsAdminService;
import com.yfp.operation.domain.dto.*;
import com.yfp.operation.domain.vo.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/statistics/admin")
@Tag(name = "后台-数据统计")
@Slf4j
public class StatisticsAdminController extends BaseController {

    @Resource
    private ComboService comboService;
    @Resource
    private StatisticsAdminService statisticsService;
    @Resource
    private MarAssociationService marAssociationService;
    @Resource
    private OrderListService orderListService;

    @GetMapping("/comboSaleData")
    @Operation(summary = "套餐销售数据-数据明细列表")
    public TableDataInfo<ComboSaleDataVO> comboSaleData(ComboSaleDataDTO comboSaleDataDTO) {
        startPage();
        List<ComboSaleDataVO> list = comboService.comboSaleData(comboSaleDataDTO);
        TableDataInfo dataTable = getDataTable(list);
        List<ComboSaleDataVO> comboSaleDataVOList = comboService.complementSaleData(list, comboSaleDataDTO);
        dataTable.setRows(comboSaleDataVOList);
        return dataTable;
    }

    @GetMapping("/pack-sales")
    @Operation(summary = "券包销售数据列表", description = "券包销售数据列表")
    public R<TableDataInfo<PackageSalesDataVO>> getPackageSalesDataList(PackageSalesDataDTO packStatisticsReq) {
        TableDataInfo<PackageSalesDataVO> packStatistics = statisticsService.getPackageSalesDataList(packStatisticsReq);
        return R.ok(packStatistics);
    }

    @PostMapping("/pack-sales/export")
    @Operation(summary = "券包销售数据列表-导出", description = "券包销售数据列表-导出")
    public void getPackageSalesDataListExport(HttpServletResponse response, @ModelAttribute PackageSalesDataDTO packStatisticsReq) {
        List<PackageSalesDataVO> packStatistics = statisticsService.getPackageSalesDataListExport(packStatisticsReq);
        ExcelUtil<PackageSalesDataVO> util = new ExcelUtil<PackageSalesDataVO>(PackageSalesDataVO.class);
        util.exportExcel(response, packStatistics, "券包销售数据");
    }

    @GetMapping("/pack-sales/single")
    @Operation(summary = "券包销售数据列表-单个券包列表", description = "券包销售数据列表-单个券包列表")
    public R<TableDataInfo<PackageSalesDataForSinglePackageVO>> getPackageSalesDataForSinglePackage(PackageSalesDataForSinglePackageDTO packStatisticsReq) {
        TableDataInfo<PackageSalesDataForSinglePackageVO> packStatistics = statisticsService.getPackageSalesDataForSinglePackage(packStatisticsReq);
        return R.ok(packStatistics);
    }

    @PostMapping("/comboDataTotal")
    @Operation(summary = "套餐销售数据-数据总览")
    public R<ComboSaleDataVO> comboDataTotal(@RequestBody ComboSaleDataDTO comboSaleDataDTO) {
        ComboSaleDataVO comboSaleDataVO = comboService.comboDataTotal(comboSaleDataDTO);
        return R.ok(comboSaleDataVO);
    }

    @GetMapping("/comboSaleInfo")
    @Operation(summary = "套餐销售数据-详情")
    public TableDataInfo<ComboSaleInfoVO> comboSaleInfo(ComboSaleDataDTO comboSaleDataDTO) {
        startPage();
        List<ComboSaleDataVO> list = comboService.comboSaleInfo(comboSaleDataDTO);
        TableDataInfo dataTable = getDataTable(list);
        List<ComboSaleInfoVO> comboSaleDataVOList = comboService.complementSaleInfo(list,comboSaleDataDTO);
        dataTable.setRows(comboSaleDataVOList);
        return dataTable;
    }

    @GetMapping("/user-layer/overview")
    @Operation(summary = "用户分层数据-数据总览", description = "用户分层数据-数据总览")
    public R<UserLayerOverviewVO> getUserLayerOverview(UserLayerDTO userLayerDTO) {
        UserLayerOverviewVO userLayerVO = statisticsService.getUserLayerOverview(userLayerDTO);
        return R.ok(userLayerVO);
    }

    @GetMapping("/user-layer")
    @Operation(summary = "用户分层数据列表", description = "用户分层数据列表")
    public R<TableDataInfo<UserLayerVO>> getUserLayerList(UserLayerDTO userLayerDTO) {
        TableDataInfo<UserLayerVO> packStatistics = statisticsService.getUserLayerList(userLayerDTO);
        return R.ok(packStatistics);
    }

    @PostMapping("/user-layer/export")
    @Operation(summary = "用户分层数据列表-导出", description = "用户分层数据列表-导出")
    public void getComboSalesListExport(HttpServletResponse response, @ModelAttribute UserLayerDTO userLayerDTO) {
        List<UserLayerVO> packStatistics = statisticsService.getUserLayerListExport(userLayerDTO);
        ExcelUtil<UserLayerVO> util = new ExcelUtil<UserLayerVO>(UserLayerVO.class);
        util.exportExcel(response, packStatistics, "用户分层数据列表");
    }

    @GetMapping("/user-layer/machine")
    @Operation(summary = "用户分层数据列表-设备", description = "用户分层数据列表-设备")
    public R<TableDataInfo<UserLayerMachineVO>> getUserLayerMachineList(UserLayerDTO userLayerDTO) {
        TableDataInfo<UserLayerMachineVO> packStatistics = statisticsService.getUserLayerMachineList(userLayerDTO);
        return R.ok(packStatistics);
    }

    @GetMapping("/user-layer/total-user")
    @Operation(summary = "用户分层数据列表-累计用户数", description = "用户分层数据列表-累计用户数")
    public R<TableDataInfo<UserLayerTotalUserVO>> getUserLayerTotalUserList(UserLayerTotalUserDTO userLayerDTO) {
        TableDataInfo<UserLayerTotalUserVO> packStatistics = statisticsService.getUserLayerTotalUserList(userLayerDTO);
        return R.ok(packStatistics);
    }

    @PostMapping("/user-layer/total-user/export")
    @Operation(summary = "用户分层数据列表-累计用户数-导出", description = "用户分层数据列表-累计用户数-导出")
    public void getUserLayerTotalUserListExport(HttpServletResponse response, @ModelAttribute UserLayerTotalUserDTO userLayerDTO) {
        List<UserLayerTotalUserVO> packStatistics = statisticsService.getUserLayerTotalUserListExport(userLayerDTO);
        ExcelUtil<UserLayerTotalUserVO> util = new ExcelUtil<UserLayerTotalUserVO>(UserLayerTotalUserVO.class);
        util.exportExcel(response, packStatistics, "用户分层数据列表-累计用户数");
    }

    @GetMapping("/user-layer/order-user")
    @Operation(summary = "用户分层数据列表-下单用户数", description = "用户分层数据列表-下单用户数")
    public R<TableDataInfo<UserLayerTotalUserVO>> getUserLayerOrderUserList(UserLayerTotalUserDTO userLayerDTO) {
        TableDataInfo<UserLayerTotalUserVO> packStatistics = statisticsService.getUserLayerOrderUserList(userLayerDTO);
        return R.ok(packStatistics);
    }

    @PostMapping("/user-layer/order-user/export")
    @Operation(summary = "用户分层数据列表-下单用户数-导出", description = "用户分层数据列表-下单用户数-导出")
    public void getUserLayerOrderUserListExport(HttpServletResponse response, @ModelAttribute UserLayerTotalUserDTO userLayerDTO) {
        List<UserLayerTotalUserVO> packStatistics = statisticsService.getUserLayerOrderUserListExport(userLayerDTO);
        ExcelUtil<UserLayerTotalUserVO> util = new ExcelUtil<UserLayerTotalUserVO>(UserLayerTotalUserVO.class);
        util.exportExcel(response, packStatistics, "用户分层数据列表-下单用户数");
    }

    @GetMapping("/user-layer/new-user")
    @Operation(summary = "用户分层数据列表-新用户数", description = "用户分层数据列表-新用户数")
    public R<TableDataInfo<UserLayerTotalUserVO>> getUserLayerNewUserList(UserLayerTotalUserDTO userLayerDTO) {
        TableDataInfo<UserLayerTotalUserVO> packStatistics = statisticsService.getUserLayerNewUserList(userLayerDTO);
        return R.ok(packStatistics);
    }

    @PostMapping("/user-layer/new-user/export")
    @Operation(summary = "用户分层数据列表-新用户数-导出", description = "用户分层数据列表-新用户数-导出")
    public void getUserLayerNewUserListExport(HttpServletResponse response, @ModelAttribute UserLayerTotalUserDTO userLayerDTO) {
        List<UserLayerTotalUserVO> packStatistics = statisticsService.getUserLayerNewUserListExport(userLayerDTO);
        ExcelUtil<UserLayerTotalUserVO> util = new ExcelUtil<UserLayerTotalUserVO>(UserLayerTotalUserVO.class);
        util.exportExcel(response, packStatistics, "用户分层数据列表-新用户数");
    }

    @GetMapping("/combo-sales")
    @Operation(summary = "商品套餐销售分析数据列表", description = "商品套餐销售分析数据列表")
    public R<TableDataInfo<ComboSalesVO>> getComboSalesList(ComboSalesDTO comboSalesDTO) {
        TableDataInfo<ComboSalesVO> comboSalesVO = statisticsService.getComboSalesList(comboSalesDTO);
        return R.ok(comboSalesVO);
    }

    @PostMapping("/combo-sales/export")
    @Operation(summary = "商品套餐销售分析数据列表-导出", description = "商品套餐销售分析数据列表-导出")
    public void getComboSalesListExport(HttpServletResponse response, @ModelAttribute ComboSalesDTO comboSalesDTO) {
        List<ComboSalesVO> packStatistics = statisticsService.getComboSalesListExport(comboSalesDTO);
        ExcelUtil<ComboSalesVO> util = new ExcelUtil<ComboSalesVO>(ComboSalesVO.class);
        util.exportExcel(response, packStatistics, "商品套餐销售分析数据");
    }

    @GetMapping("/combo-sales/position")
    @Operation(summary = "商品套餐销售分析数据列表-投放点位", description = "商品套餐销售分析数据列表-投放点位")
    public R<TableDataInfo<ComboSalesPositionVO>> getComboSalesPositionList(ComboSalesPositionDTO comboSalesDTO) {
        TableDataInfo<ComboSalesPositionVO> comboSalesVO = statisticsService.getComboSalesPositionList(comboSalesDTO);
        return R.ok(comboSalesVO);
    }

    @GetMapping("/associationDataInfo")
    @Operation(summary = "社群数据明细列表")
    public TableDataInfo<AssociationVO> associationDataInfo(AssociationDTO associationDTO) {
        log.info("参数:{}", associationDTO);
        DateTime dateTime = new DateTime();
        startPage();
        List<AssociationVO> list = marAssociationService.associationDataInfo(associationDTO);
        log.info("数据列表:{}",list);
        TableDataInfo dataTable = getDataTable(list);
        List<AssociationVO> associationVOS = marAssociationService.complementDataInfo(list, associationDTO);
        dataTable.setRows(associationVOS);
        DateTime dateTime1 = new DateTime();
        long l = dateTime.getTime() - dateTime1.getTime();
        log.info("耗时:{}",l);
        return dataTable;
    }


    @GetMapping("/positionAssociation")
    @Operation(summary = "点位社群列表")
    public TableDataInfo<AssociationVO> positionAssociation(AssociationDTO associationDTO) {
        startPage();
        List<AssociationVO> list = marAssociationService.positionAssociation(associationDTO);
        TableDataInfo dataTable = getDataTable(list);
        List<AssociationVO> associationVOS = marAssociationService.complementPositionAssociation(list, associationDTO);
        dataTable.setRows(associationVOS);
        return dataTable;
    }


    @GetMapping("/dealDataList")
    @Operation(summary = "交易数据列表")
    public TableDataInfo<DealDataVO> dealDataList(DealDataDTO dealDataDTO) {
        startPage();
        List<DealDataVO> list = orderListService.dealDataList(dealDataDTO);
        TableDataInfo dataTable = getDataTable(list);
        return dataTable;
    }

    @PostMapping("/dealDataTotal")
    @Operation(summary = "交易数据-总览")
    public R<DealDataVO> dealDataTotal(@RequestBody DealDataDTO dealDataDTO) {
        DealDataVO dealDataVO = orderListService.dealDataTotal(dealDataDTO);
        return R.ok(dealDataVO);
    }

    @PostMapping("/comboSaleData/derive")
    @Operation(summary = "套餐销售数据列表数据导出", description = "数据导出")
    @Log(title = "套餐销售数据列表数据导出", businessType = BusinessType.EXPORT)
    public void comboSaleDataDerive(HttpServletResponse response, @ModelAttribute ComboSaleDataDTO comboSaleDataDTO) {
        List<ComboSaleDataVO> list = comboService.comboSaleData(comboSaleDataDTO);
        List<ComboSaleDataVO> comboSaleDataVOList = comboService.complementSaleData(list, comboSaleDataDTO);
        ExcelUtil<ComboSaleDataVO> util = new ExcelUtil<ComboSaleDataVO>(ComboSaleDataVO.class);
        util.exportExcel(response, comboSaleDataVOList, "套餐销售数据");
    }


    @PostMapping("/comboSaleInfo/derive")
    @Operation(summary = "套餐销售数据详情数据导出", description = "数据导出")
    @Log(title = "套餐销售数据详情数据导出", businessType = BusinessType.EXPORT)
    public void comboSaleInfoDerive(HttpServletResponse response, @ModelAttribute ComboSaleDataDTO comboSaleDataDTO) {
        List<ComboSaleDataVO> list = comboService.comboSaleInfo(comboSaleDataDTO);
        List<ComboSaleInfoVO> comboSaleDataVOList = comboService.complementSaleInfo(list,comboSaleDataDTO);
        ExcelUtil<ComboSaleInfoVO> util = new ExcelUtil<ComboSaleInfoVO>(ComboSaleInfoVO.class);
        util.exportExcel(response, comboSaleDataVOList, "套餐销售详情数据");
    }



    @PostMapping("/dealDataList/derive")
    @Operation(summary = "交易数据列表数据导出", description = "数据导出")
    @Log(title = "交易数据列表数据导出", businessType = BusinessType.EXPORT)
    public void derive(HttpServletResponse response, @ModelAttribute DealDataDTO dealDataDTO) {
        List<DealDataVO> list = orderListService.dealDataList(dealDataDTO);
        ExcelUtil<DealDataVO> util = new ExcelUtil<DealDataVO>(DealDataVO.class);
        util.exportExcel(response, list, "交易数据");
    }

    @PostMapping("/associationDataInfo/derive")
    @Operation(summary = "社群数据明细列表数据导出", description = "数据导出")
    @Log(title = "社群数据明细列表数据导出", businessType = BusinessType.EXPORT)
    public void associationDataInfoDerive(HttpServletResponse response, @ModelAttribute AssociationDTO associationDTO) {
        List<AssociationVO> list = marAssociationService.associationDataInfo(associationDTO);
        List<AssociationVO> associationVOS = marAssociationService.complementDataInfo(list, associationDTO);
        ExcelUtil<AssociationVO> util = new ExcelUtil<AssociationVO>(AssociationVO.class);
        util.exportExcel(response, associationVOS, "交易数据");
    }




}
